<template>
  <el-dialog title="积分路由配置" :visible.sync="visible" width="500px" @closed="reset">
    <h4>积分统计路由方式:</h4>
    <p><el-radio v-model="radio" label="1">不统计积分(默认)</el-radio></p>
    <p>
      <el-radio v-model="radio" label="2" style="margin-right: 0;" @change="radioChange">按订单金额统计，</el-radio>
      <el-input-number v-model="amount" :disabled="radio!=='2'" :min="1" :max="99999999" :precision="0" :controls="false" style="width: 100px;" />元赠送
      <el-input-number v-model="score" :disabled="radio!=='2'" :min="1" :max="99999999" :precision="0" :controls="false" style="width: 100px;" />个积分
    </p>
    <p><el-radio v-model="radio" label="3"> 按商品设置积分统计</el-radio></p>
    <el-divider />
    <h4>请勾选积分所支持的订单渠道来源:</h4>
    <p><el-checkbox v-model="source.phone">呼叫中心</el-checkbox></p>
    <p><el-checkbox v-model="source.wechat">微信</el-checkbox></p>
    <p><el-checkbox v-model="source.distribution">配送员</el-checkbox></p>
    <p><el-checkbox v-model="source.store">门店</el-checkbox></p>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { querySetScoreRoute, queryScoreRouteDetail } from '@/api/index'

  export default {
    name: 'IntegraRouting',
    data() {
      return {
        visible: false,
        detail: {
          routerOption: 0
        },
        source: {
          phone: false,
          wechat: false,
          distribution: false,
          store: false
        },
        radio: '2',
        amount: undefined,
        score: undefined
      }
    },
    methods: {
      async open() {
        await this.getDeaultData()
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
        })
      },
      async getDeaultData() {
        const res = await queryScoreRouteDetail()
        this.detail = res.data
        if (res.data.routerCode) this.detail.routerOption = res.data.routerCode
        if ((this.detail.routerOption & 0x01) === 1) {
          this.radio = '2'
          this.amount = res.data.amount
          this.score = res.data.score
        } else if ((this.detail.routerOption & 0x02) === 2) {
          this.radio = '3'
          this.amount = undefined
          this.score = undefined
        } else {
          this.radio = '1'
          this.amount = undefined
          this.score = undefined
        }
        if (this.detail.routerOption >> 16 & 0x01) this.source.phone = true
        if (this.detail.routerOption >> 17 & 0x01) this.source.wechat = true
        if (this.detail.routerOption >> 18 & 0x01) this.source.distribution = true
        if (this.detail.routerOption >> 19 & 0x01) this.source.store = true
      },
      radioChange(value) {
        if (value === '2') {
          this.amount = 1
          this.score = 1
        }
      },
      success() {
        let routerOption = 0
        routerOption = this.radio === '1' ? 0 : routerOption
        routerOption = this.radio === '2' ? 1 : routerOption
        routerOption = this.radio === '3' ? 2 : routerOption
        if (routerOption === 1) {
          if (!this.amount) {
            this.$message.error('请输入金额')
            return
          }
          if (!this.score) {
            this.$message.error('请输入赠送积分')
            return
          }
        }
        if (this.source.phone) routerOption |= (1 << 16)
        if (this.source.wechat) routerOption |= (1 << 17)
        if (this.source.distribution) routerOption |= (1 << 18)
        if (this.source.store) routerOption |= (1 << 19)
        const params = {
          routerOption,
          amount: this.amount,
          score: this.score
        }
        this.submit(params)
      },
      async submit(params) {
        await querySetScoreRoute(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.source = {
          phone: false,
          wechat: false,
          distribution: false,
          store: false
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
.deletePage {
  min-height: 100px;
  text-align: center;
}
p { line-height: 2rem; }
</style>
